package mysgoog.brown.basicaction;

import mysgoog.brown.*;
import mysgoog.brown.dragon.*;

public class BasActionMgr
{
    static private boolean[] covering = new boolean[Brown.maxSize];
    static private boolean[] visiting = new boolean[Brown.maxSize];
    static int mycolor = 0;
    static int list = 0;
    static int ucolor;
    static int val = 0;
    static public Answer[] ret = null;

    static public int maxlib = 4;

    static public void go(int mc)
    {
        mycolor = mc;
        ucolor = Brown.OTHER_COLOR(mycolor);
        for (int i = 0; i < Brown.maxSize; i++)
        {
            covering[i] = false;
            visiting[i] = false;
        }
        tryattack();
        trydefense();
    }

    static private void trydefense()
    {
        int m, n, minlib;
        int val = 0;
        int[] tmpboard = Brown.board;
        int origin;
        for (minlib = 1; minlib < maxlib; minlib++)
        {
            for (m = 0; m < Brown.maxSize; m++)
                for (n = 0; n < Brown.maxSize; n++)
                    if (tmpboard[m * Brown.maxSize + n - Brown.maxSize] == mycolor)
                    {
                        origin = DragonMgr.worms[m * Brown.maxSize + n - Brown.maxSize].origin;
                        if ((!covering[origin]) && (DragonMgr.worms[origin].libn == minlib))
                        {
                            covering[origin] = true;
                            findnextmove(origin, minlib);
                        }
                    }
        }
    }

    static private void tryattack()
    {
        int origin;
        int free;
        int[] tmpboard = Brown.board;
        int lib;
        int tval;
        for (int i = 0; i < Brown.maxSize; i++)
            covering[i] = false;
        for (int m = 0; m < Brown.maxSize; m++)
            for (int n = 0; n < Brown.maxSize; n++)
            {
                origin = DragonMgr.worms[m * Brown.maxSize + n - Brown.maxSize].origin;
                if (Brown.get_board(m, n) == ucolor)
                {
                    if (!covering[origin])
                    {
                        covering[origin] = true;
                        free = DragonMgr.worms[origin].libn;
                        if (free < maxlib)
                        {
                            if (free == 1)
                            {
                                list++;
                                ret[list] = new Answer(DragonMgr.worms[origin].libs[0], 120);
                            } else
                                for (int u = 0; u < (int) free; u++)
                                    for (int v = 0; v < (int) free; v++)
                                        if (u != v)
                                        {
                                            lib = 0;
                                            tmpboard[u] = mycolor;
                                            for (int tmp = 0; tmp < Brown.maxSize; tmp++)
                                                visiting[tmp] = false;
                                            int lib1 = countlib(origin);
                                            tmpboard[v] = ucolor;
                                            for (int tmp = 0; tmp < Brown.maxSize; tmp++)
                                                visiting[tmp] = false;
                                            int lib2 = countlib(origin);

                                            if ((lib1 == 1) && (lib2 > 0))
                                                tval = 0;
                                            else
                                                tval = 120 - 20 * lib;
                                            boolean flag = false;
                                            if (val < tval)
                                            {
                                                val = tval;
                                                flag = true;
                                            }
                                            if (tval > 60)
                                                flag = true;

                                            if (flag)
                                            {
                                                list++;
                                                ret[list] = new Answer(u * Brown.maxSize + v
                                                        - Brown.maxSize, tval);
                                            }

                                            tmpboard[u] = Brown.EMPTY;
                                            tmpboard[v] = Brown.EMPTY;
                                        }
                        }
                    }
                }
            }
    }

    static private int countlib(int place)
    {
        int ret = 0;
        if (visiting[place])
            return 0;
        visiting[place] = true;
        if (Brown.board[place] == Brown.EMPTY)
            return 1;
        ret = ret + countlib(place - 1);
        ret = ret + countlib(place + 1);
        ret = ret + countlib(place - Brown.MAX_BOARD);
        ret = ret + countlib(place + Brown.MAX_BOARD);
        return ret;
    }

    static private void findnextmove(int place, int minlib)
    {
        int[] tmpboard = Brown.board;
        if (Brown.board[place] == Brown.EMPTY)
        {
            tmpboard[place] = mycolor;
            for (int i = 0; i < Brown.maxSize; i++)
                visiting[i] = false;
            int lib = countlib(place);
            int tmp = 40 + (lib - minlib - 1) * 50 / (minlib * minlib * minlib);
            if (tmp > 60)
            {
                list++;
                ret[list] = new Answer(place, tmp);
            }
            tmpboard[place] = ucolor;
        } else
        {
            if (Brown.board[place] == ucolor)
                return;
            findnextmove(place - 1, minlib);
            findnextmove(place + 1, minlib);
            findnextmove(place - Brown.MAX_BOARD, minlib);
            findnextmove(place + Brown.MAX_BOARD, minlib);
        }
    }

    static public int getnumber()
    {
        return list;
    }

    static public Answer[] getPlace()
    {
        return ret;
    }
}